import { _decorator, Component, error, Label, Node, ProgressBar, Slider } from 'cc';
import { UIContianer } from './UIContianer';
import { UIComp } from './UIConfig';
const { ccclass, property } = _decorator;
let refreshLabel = (oldValue:any,newValue:any,comp:Component)=>{
    (comp as Label).string = newValue;
}
let refreshProgress = (oldValue:any,newValue:any,comp:Component)=>{
    (comp as ProgressBar).progress = newValue;
}
let refreshSlider = (oldValue:any,newValue:any,comp:Component)=>{
    (comp as Slider).progress = newValue;
}
const CompCallback = {
    Label:refreshLabel,
    ProgressBar:refreshProgress,
    Slider:refreshSlider
}


@ccclass('UIUtils')
export class UIUtlis  {
    static findNode(node:Node,uiContianer:UIContianer){
        if(!node||!uiContianer){
            return;
        }
        let nodeName=node.name;
        if(nodeName.startsWith('_')){
            uiContianer.addNode(nodeName,node);
            for(let compt in UIComp){
                let compType=UIComp[compt];
                let comp=node.getComponent(compType);
                if(!comp){
                    continue;
                }
                uiContianer.addComp(node.name,compt,comp)

            }
        }
        for(let child of node.children){
            this.findNode(child,uiContianer);
        }
    }
    static refreshComp(oldValue:any,newValue:any,comp:Component){
        if(!comp){
            error('刷新组件失败，组件不存在');
            return;
        }
        for(let compTypeName in UIComp){
            if(comp instanceof UIComp[compTypeName]){
                CompCallback[compTypeName](oldValue,newValue,comp);
                return;
            }
        }
    }
}


